package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/api/core/userInfo")

public class ApiUserInfoController {
    @Resource
    private UserInfoService userInfoService;
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO ){

        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        boolean b = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(b, ResponseEnum.MOBILE_ERROR);
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);


        boolean b1 = userInfoService.checkCode(registerVO);
        Assert.isTrue(b1,ResponseEnum.CODE_ERROR);

        userInfoService.register(registerVO);
        return R.ok();
    }
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request){

        String ipRemote = request.getRemoteAddr();//nginx自己的ip
        String header = request.getHeader("X-forwarded-for");// nginx转发的客户端ip

        UserInfoVO userInfoVO = userInfoService.login(loginVO,ipRemote);

        return R.ok().data("userInfoVO",userInfoVO);
    }

    @ApiOperation("手机重复注册校验功能")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable("mobile") String mobile){
        boolean b = userInfoService.checkMobile(mobile);
        return b;
    }
    @GetMapping("/test")
    public String test(){
//        int i = 1/0;
        System.out.println("provider执行业务请求");
        return "provider-test";
    }
}
